package com.we.flink.dcapiuserlabel.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.dcapiuserlabel.table.TableUserAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TagUserAction {
    public long uid;
    public int enter_dc_supply_info_page_time;
    public int enter_dc_contract_page_time;
    public int enter_baoguo_info_page_time;
    public int enter_tiecard_detail_page_time;
    public int enter_baoguo_pay_page_time;
    public int dc_audit_success_time;
    public int dc_audit_fail_time;
    public int dc_contract_fail_time;
    public int dc_withdraw_time;
    public int dc_submit_withdraw_time;
    public int upload_cmpy_img_time;
    public int cmpy_img_ocr_success_time;
    public int cmpy_success_identity_time;
    public int enter_biguocard_info_page_time;
    public int enter_biguocard_pay_page_time;
    public int enter_dc_yxcd_page_time;

    public static Logger LOG = LoggerFactory.getLogger(TagUserAction.class);

    public TagUserAction(
            long uid,
            int enter_dc_supply_info_page_time,
            int enter_dc_contract_page_time,
            int enter_baoguo_info_page_time,
            int enter_tiecard_detail_page_time,
            int enter_baoguo_pay_page_time,
            int dc_audit_success_time,
            int dc_audit_fail_time,
            int dc_contract_fail_time,
            int dc_withdraw_time,
            int dc_submit_withdraw_time,
            int upload_cmpy_img_time,
            int cmpy_img_ocr_success_time,
            int cmpy_success_identity_time,
            int enter_biguocard_info_page_time,
            int enter_biguocard_pay_page_time,
            int enter_dc_yxcd_page_time) {
        this.uid = uid;
        this.enter_dc_supply_info_page_time = enter_dc_supply_info_page_time;
        this.enter_dc_contract_page_time = enter_dc_contract_page_time;
        this.enter_baoguo_info_page_time = enter_baoguo_info_page_time;
        this.enter_tiecard_detail_page_time = enter_tiecard_detail_page_time;
        this.enter_baoguo_pay_page_time = enter_baoguo_pay_page_time;
        this.dc_audit_success_time = dc_audit_success_time;
        this.dc_audit_fail_time = dc_audit_fail_time;
        this.dc_contract_fail_time = dc_contract_fail_time;
        this.dc_withdraw_time = dc_withdraw_time;
        this.dc_submit_withdraw_time = dc_submit_withdraw_time;
        this.upload_cmpy_img_time = upload_cmpy_img_time;
        this.cmpy_img_ocr_success_time = cmpy_img_ocr_success_time;
        this.cmpy_success_identity_time = cmpy_success_identity_time;
        this.enter_biguocard_info_page_time = enter_biguocard_info_page_time;
        this.enter_biguocard_pay_page_time = enter_biguocard_pay_page_time;
        this.enter_dc_yxcd_page_time = enter_dc_yxcd_page_time;
    }

    public static TagUserAction init(long uid) {
        return new TagUserAction(uid, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableUserAction.QUERYSQL);
        DataStream<Tuple2<Boolean, TableUserAction>> inputStream =
                tableEnv.toRetractStream(tableRes, TableUserAction.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableUserAction>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableUserAction> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableUserAction>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableUserAction> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableUserAction>, String>() {
                                    ValueState<TagUserAction> userActionTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        userActionTagVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagUserAction>(
                                                                        "UserActionTagVState",
                                                                        TagUserAction.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableUserAction> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        String type = value.f1.getType();
                                        int actionTime =
                                                (int) (value.f1.getAction_time().getTime() / 1000);

                                        TagUserAction tmpUserAction = userActionTagVState.value();
                                        TagUserAction curUserAction =
                                                (tmpUserAction == null
                                                        ? TagUserAction.init(uid)
                                                        : tmpUserAction);

                                        JSONObject jsonObject = new JSONObject();
                                        jsonObject.put("uid", uid);
                                        switch (type) {
                                            case "ENTER_DC_SUPPLY_INFO_PAGE":
                                                if (curUserAction.enter_dc_supply_info_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_dc_supply_info_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_dc_supply_info_page_time",
                                                            curUserAction
                                                                    .enter_dc_supply_info_page_time);
                                                }
                                                break;
                                            case "ENTER_DC_CONTRACT_PAGE":
                                                if (curUserAction.enter_dc_contract_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_dc_contract_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_dc_contract_page_time",
                                                            curUserAction
                                                                    .enter_dc_contract_page_time);
                                                }
                                                break;
                                            case "ENTER_BAOGUO_INFO_PAGE":
                                                if (curUserAction.enter_baoguo_info_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_baoguo_info_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_baoguo_info_page_time",
                                                            curUserAction
                                                                    .enter_baoguo_info_page_time);
                                                }
                                                break;
                                            case "ENTER_BAOGUO_PAY_PAGE":
                                                if (curUserAction.enter_baoguo_pay_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_baoguo_pay_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_baoguo_pay_page_time",
                                                            curUserAction
                                                                    .enter_baoguo_pay_page_time);
                                                }
                                                break;
                                            case "ENTER_TIECARD_DETAIL_PAGE":
                                                if (curUserAction.enter_tiecard_detail_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_tiecard_detail_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_tiecard_detail_page_time",
                                                            curUserAction
                                                                    .enter_tiecard_detail_page_time);
                                                }
                                                break;
                                            case "DC_AUDIT_SUCCESS":
                                                if (curUserAction.dc_audit_success_time
                                                        < actionTime) {
                                                    curUserAction.dc_audit_success_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "dc_audit_success_time",
                                                            curUserAction.dc_audit_success_time);
                                                }
                                                break;
                                            case "DC_AUDIT_FAIL":
                                                if (curUserAction.dc_audit_fail_time < actionTime) {
                                                    curUserAction.dc_audit_fail_time = actionTime;
                                                    jsonObject.put(
                                                            "dc_audit_fail_time",
                                                            curUserAction.dc_audit_fail_time);
                                                }
                                                break;
                                            case "DC_CONTRACT_FAIL":
                                                if (curUserAction.dc_contract_fail_time
                                                        < actionTime) {
                                                    curUserAction.dc_contract_fail_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "dc_contract_fail_time",
                                                            curUserAction.dc_contract_fail_time);
                                                }
                                                break;
                                            case "DC_WITHDRAW":
                                                if (curUserAction.dc_withdraw_time < actionTime) {
                                                    curUserAction.dc_withdraw_time = actionTime;
                                                    jsonObject.put(
                                                            "dc_withdraw_time",
                                                            curUserAction.dc_withdraw_time);
                                                }
                                                break;
                                            case "DC_SUBMIT_WITHDRAW":
                                                if (curUserAction.dc_submit_withdraw_time
                                                        < actionTime) {
                                                    curUserAction.dc_submit_withdraw_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "dc_submit_withdraw_time",
                                                            curUserAction.dc_submit_withdraw_time);
                                                }
                                                break;
                                            case "CLICKE_ADD_COMPANY_IMG":
                                                if (curUserAction.upload_cmpy_img_time
                                                        < actionTime) {
                                                    curUserAction.upload_cmpy_img_time = actionTime;
                                                    jsonObject.put(
                                                            "upload_cmpy_img_time",
                                                            curUserAction.upload_cmpy_img_time);
                                                }
                                                break;
                                            case "COMPANY_IMG_OCR_SUCCESS":
                                                if (curUserAction.cmpy_img_ocr_success_time
                                                        < actionTime) {
                                                    curUserAction.cmpy_img_ocr_success_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "cmpy_img_ocr_success_time",
                                                            curUserAction
                                                                    .cmpy_img_ocr_success_time);
                                                }
                                                break;
                                            case "COMPANY_SUCCESS_IDENTITY":
                                                if (curUserAction.cmpy_success_identity_time
                                                        < actionTime) {
                                                    curUserAction.cmpy_success_identity_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "cmpy_success_identity_time",
                                                            curUserAction
                                                                    .cmpy_success_identity_time);
                                                }
                                                break;
                                            case "ENTER_BIGUOCARD_INFO_PAGE":
                                                if (curUserAction.enter_biguocard_info_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_biguocard_info_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_biguocard_info_page_time",
                                                            curUserAction
                                                                    .enter_biguocard_info_page_time);
                                                }
                                                break;
                                            case "ENTER_BIGUOCARD_PAY_PAGE":
                                                if (curUserAction.enter_biguocard_pay_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_biguocard_pay_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_biguocard_pay_page_time",
                                                            curUserAction
                                                                    .enter_biguocard_pay_page_time);
                                                }
                                                break;
                                            case "DC_ENTER_YXCD_PAGE":
                                                if (curUserAction.enter_dc_yxcd_page_time
                                                        < actionTime) {
                                                    curUserAction.enter_dc_yxcd_page_time =
                                                            actionTime;
                                                    jsonObject.put(
                                                            "enter_dc_yxcd_page_time",
                                                            curUserAction.enter_dc_yxcd_page_time);
                                                }
                                                break;
                                            default:
                                                System.out.println(
                                                        "uid: "
                                                                + uid
                                                                + " Not support this type: "
                                                                + type);
                                                break;
                                        }

                                        if (jsonObject.size() > 1) {
                                            out.collect(jsonObject.toString());
                                            userActionTagVState.update(curUserAction);
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }
}
